"use strict";

const BaseModel = require("../basics/mysql/mapper");
const Constant = require("../basics/constant");
class HomeModel extends BaseModel {
  constructor(cloud) {
    super(cloud, "prize_record");
  }

  /**
   * 获取实例
   * @param {*} cloud 
   */
  static getInstant(cloud) {
    return new HomeModel(cloud);
  }

  /**
   * 查询列表
   * @param {*} openId 
   */
  async selectList(openId) {
    const ex = this.createExample();
    ex.criteria.andEqual("openId", openId);
    ex.setColumns(["id", "code", "name", "type", "status"]);
    ex.setOrder("type", "asc");
    return await this.selectListByExample(ex);
  }

  /**
   * 查询单条
   * @param {*} openId 
   * @param {*} type 
   */
  async selectOne(openId, type) {
    const ex = this.createExample();
    ex.criteria.andEqual("openId", openId).andEqual("type", type);
    return await this.selectOneByExample(ex);
  }


  /**
   * 已获取
   * @param {*} id 
   */
  async obtain(id) {
    const sql = `update prize_record set status=0 where id=${id} and status=-1`;
    const data = await this.mfSql(sql, []);
    return data.affectedRows;
  }

  /**
   * 领取中
   * @param {*} id 
   */
  async collect(id) {
    const sql = `update prize_record set status=3 where id=${id} and status=0`;
    const data = await this.mfSql(sql, []);
    return data.affectedRows;
  }

  /**
 * 回滚
 * @param {*} id 
 */
  async back(id) {
    const sql = `delete from prize_record where id=${id} and status=-1`;
    const data = await this.mfSql(sql, []);
    return data.affectedRows;
  }
}

module.exports = HomeModel;